package com.system.service.impl;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.common.base.persistence.Page;
import com.github.pagehelper.PageHelper;
import com.system.mapper.SysRoleMapper;
import com.system.mapper.SysStaffMapper;
import com.system.service.ISysStaffService;
import com.system.vo.SysRoleVO;
import com.system.vo.SysStaffVO;

import jxl.common.Logger;

@Service("staffService")
public class SysStaffServiceImpl implements ISysStaffService{
	
	private static Logger logger = Logger.getLogger(SysStaffServiceImpl.class);
	
	@Resource
	private SysStaffMapper staffMapper;
	
	@Resource
	private SysRoleMapper roleMapper;
	
	@Override
	public List<SysStaffVO> getStaffList(Page<SysStaffVO> page,SysStaffVO sysStaffVO) {
	
		return page.getList();
	}
	
	/**
	 * @方法说明 
	 *  通过员工编号查询员工基本信息
	 * @param staff_id :员工编号
	 * @创建日期 2017年4月19日
	 */
	@Override
	public SysStaffVO queryStaffDetail(String staff_id) {
		SysStaffVO sysStaffVO = staffMapper.queryStaffDetail(staff_id);
		return sysStaffVO;
	}

	/**
	 * @方法说明
	 *  通过员工编号查询员工基本信息
	 * @param staff_id :员工编号
	 * @创建日期 2017年8月25日
	 */
	@Override
	public int countStaffById(String staff_id) {
		int count = staffMapper.countStaffById(staff_id);
		return count;
	}
	/**
	 * 根据员工姓名，模糊搜索员工
	 * @param staff_name ：员工姓名
	 * @return List<SysStaffVO> ：满足条件的员工集合
	 */
	@Override
	public List<SysStaffVO> queryStaffByName(String staff_name) {
		
		try {
			if(staff_name != null && !"".equals(staff_name)){
				
				List<SysStaffVO> list = staffMapper.queryStaffByName(staff_name);
				logger.info("根据员工姓名【"+staff_name+"】模糊搜索到员工 "+list != null ? list.size() : 0 +" 条");
				
				return list;
			}else{
				logger.warn("根据员工姓名模糊搜索员工需要的参数【员工姓名】为Null，不能查询！");
				return null;
			}
		} catch (Exception e) {
			logger.error("根据员工姓名【"+staff_name+"】模糊搜索员工异常",e);
		}
		return null;
	}

	@Override
	public Map<String, String> queryNameId() {
		try {
			logger.info("开始查询所有员工的姓名、Id");
			List<SysStaffVO> list = staffMapper.queryNameId();
			logger.info("查询所有员工的姓名、Id成功，共查询到【"+list.size()+"】条记录");
			Map<String, String> map = new HashMap<String,String>();
			for(SysStaffVO m:list){
				map.put(m.getStaffId(),m.getStaffName());
			}
			return map;
		} catch (Exception e) {
			logger.error("查询所有员工姓名、Id异常",e);
			return null;
		}
	}

	@Override
	public SysRoleVO findRoleById(String roleId) {
		// TODO Auto-generated method stub
		return roleMapper.findRoleById(roleId);
	}

	@Override
	public List<String> findMenuIdByRoleId(String roleId) {
		// TODO Auto-generated method stub
		return roleMapper.findPermissions(roleId);
	}

	@Override
	public List<SysStaffVO> findStaffSBymap(Map map, Page<SysStaffVO> page) {
		int start = page.getPage();
		int pageSize = page.getRows();
		PageHelper.startPage(start,pageSize,true);  //第一页 每页显示10条
		List<SysStaffVO> list=staffMapper.findStaffSBymap(map);
		page.loadData(list);
		return page.getList();
	}

	@Override
	public void editStaffInfoAndRoles(SysStaffVO staff, String[] roleIds)
			throws Exception {
		// TODO Auto-generated method stub
		String staffName=staff.getStaffName();
		List<String>roleIdsList=null;
		if(roleIds!=null&&roleIds.length>0){
			roleIdsList=Arrays.asList(roleIds);
		}
		Map<String, Object>updatemap=new HashMap<String, Object>();
		updatemap.put("staffName", staffName);
		updatemap.put("staffId", staff.getStaffId());
	    staffMapper.deleteStaffAndRoles(staff.getStaffId());
	    if(roleIdsList!=null&&roleIdsList.size()>0){
		   for (String roleId : roleIdsList) {
		    	staffMapper.addStaffAndRoles(staff.getStaffId(), roleId);
			}
	   }
	    staffMapper.editStaffInfo(updatemap);
	}

	@Override
	public List<SysRoleVO> findRolesByUserId(String staff_id) {
		return staffMapper.findRolesByUserId(staff_id);
	}

	@Override
	public SysStaffVO findById(String staffId) {
		SysStaffVO staff = staffMapper.findById(staffId);
		return staff;
	}
	@Override
	public String findRolesIdByStaffId(String staffId) {
		return staffMapper.findRolesIdByStaffId(staffId);
	}

	/**
	 * 添加员工
	 * @param vo
	 */
	@Override
	public boolean  addStaff(SysStaffVO vo){
		try {
			staffMapper.addStaff(vo);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 删除员工
	 * @param staffId 员工编号
	 */
	public boolean deleteStaff(String staffId){
		try {
			staffMapper.deleteStaff(staffId);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 删除用户下的所有角色
	 * @param staffId
	 * @return
	 */
	public boolean deleteStaffRoles(String staffId){
		try {
			staffMapper.deleteStaffAndRoles(staffId);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 修改员工
	 * @param vo
	 * @throws SQLException
	 */
	public boolean updateStaff(SysStaffVO vo){
		try {
			staffMapper.updateStaff(vo);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public SysStaffVO queryStaffOne(String staffId) {
		SysStaffVO sysStaffVO = new SysStaffVO();
		sysStaffVO.setStaffId(staffId);
		try {
			sysStaffVO = staffMapper.queryStaffOne(sysStaffVO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sysStaffVO;
	}

	@Override
	public void modifiyPsw(String newPsw,String staffId) throws Exception {
		staffMapper.modifiyPsw(newPsw,staffId);
	}
	
	
	
	public SysStaffVO  findAccountByName(String staffName){
		return staffMapper.findAccountByName(staffName);
	}
}
